<template>
  <div>
    <div>分配权限功能</div>
    <!-- 树形控件 -->
    <el-tree ref="tree" node-key="id" :data="permissionList" show-checkbox default-expand-all highlight-current :props="{ label: 'name' }" check-strictly />
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="cancelButton">取 消</el-button>
        <el-button size="small" type="primary" @click="setRolesBtn">确 定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'AssignPermission',
  props: {
    // 权限列表
    permissionList: {
      type: Array,
      default: _ => []
    },
    // 当前点击项的id
    roleId: {
      type: [Number, String],
      required: true
    },
    // 当前角色的权限
    roleIdsList: {
      type: Array,
      default: _ => []
    }
  },
  data() {
    return {}
  },
  watch: {
    // 监听当前角色的权限数组的变化,使用组件内置的方法设置选中的id
    roleIdsList() {
      this.$refs.tree.setCheckedKeys(this.roleIdsList)
    }
  },
  methods: {
    // 取消按钮
    cancelButton() {
      this.$emit('update:dialogVisible', false)
    },

    // 确定设置权限按钮
    setRolesBtn() {
      // console.log(this.$refs.tree.getCheckedKeys())
      const permIds = this.$refs.tree.getCheckedKeys() // 获取被勾选中的权限的id
      this.$emit('confirmPerEV', { id: this.roleId, permIds: permIds }) // 传给父组件
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
